package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Utiles {
    public Utiles() {
    }
    
   //static final Logger logger = Utiles.iniciarLogger("Utiles", true);
    
    public static Logger iniciarLogger(String nombreClase,
            boolean mostrarEnConsola) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        
        Logger logger = Logger.getLogger(nombreClase);
        
        StringBuilder outputLogPath = new StringBuilder();
        outputLogPath.append("/logMicrositios/Log_");
        outputLogPath.append(logger.getName());
        outputLogPath.append("_");
        outputLogPath.append(calendar.get(Calendar.DATE));
        outputLogPath.append("_");
        outputLogPath.append(calendar.get(Calendar.MONTH));
        outputLogPath.append("_");
        outputLogPath.append(calendar.get(Calendar.YEAR));
        outputLogPath.append(".xml");
        logger.setUseParentHandlers(mostrarEnConsola);
        
        try {
            FileHandler fh = new FileHandler(outputLogPath.toString());
            fh.setLevel(Level.ALL);
            logger.addHandler(fh);
        } catch (IOException e) {
            logger.log(Level.SEVERE,e.getMessage(),e);
        }
        return logger;
    }
    
    /**
     * Convierte la primer letra de una cadena en una Mayuscula las demas en minusculas
     * @param texto
     * @return
     */
    public static String formatearTexto(String texto) {
        StringBuilder t = new  StringBuilder();
        String t1 = new  String();
        String letra;
        try {
            
            t.append(texto);
            
            letra = t.substring(0,1).toUpperCase();
            t.delete(0,1);
            t1=t.toString().toLowerCase();
            t.setLength(0);
            t.append(t1);
            t.insert(0,letra);
        } catch (Exception e) {
           // logger.log(Level.SEVERE,e.getMessage(),e);
        }
        return t.toString();
    }
    
    public static String noNull(Object o, String retorno){
        String s=retorno;
        try {
            if(o!=null)
                //no se puede castear un int a un String sin usar el metodo valueOf
                s = String.valueOf(o);
        } catch (Exception e) {
            s = retorno;
        }
        return s;
    }
    
    public static String noNull(Object o){
        String s="";
        try {
            if(o!=null)
                //no se puede castear un int a un String sin usar el metodo valueOf
                s = String.valueOf(o);
        } catch (Exception e) {
            s = "";
        }
        return s;
    }
    
    public static void copiarDirectorio(File srcPath, File dstPath) throws IOException{
        if (srcPath.isDirectory()) {
            if (!dstPath.exists()) {
                dstPath.mkdir();
            }
            
            String files[] = srcPath.list();
            for(int i = 0; i < files.length; i++) {
                copiarDirectorio(new File(srcPath, files[i]), new File(dstPath, files[i]));
            }
        } else {
            if(!srcPath.exists()) {
                System.out.println("File or directory does not exist.");
                System.exit(0);
            } else {
                InputStream in = new FileInputStream(srcPath);
                OutputStream out = new FileOutputStream(dstPath);
                
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }
        }
        System.out.println("Directory copied.");
    }
    
    public static void borrarDirectorio(File srcPath) throws IOException{
        if (srcPath.isDirectory()) {
            String files[] = srcPath.list();
            for(int i = 0; i < files.length; i++) {
                borrarDirectorio(new File(srcPath, files[i]));
            }
        } else {
            srcPath.delete();
        }
    }
}
